
import logging
import configparser
from conf import setting
import os

class OperationConfig:
    def __init__(self, filepath=None) -> None:
        if filepath is None:
            self.__filepath = setting.FILE_PATH['CONFIG']
        else:
            self.__filepath = filepath
        try:
            self.conf = configparser.ConfigParser()
            self.conf.read(self.__filepath)
        except Exception as e:
            logging.error(e)

    def get_section_for_data(self, section, option) -> str:
        try:
            values = self.conf.get(section, option)
            return values
        except Exception as e:
            logging.error(e)
            return ""

    def write_config_data(self, section, option_key, option_value):
        if section not in self.conf.sections():
            self.conf.add_section(section)
            self.conf.set(section, option_key, option_value)
        else:
            logging.info(f'"{section}"值已存在，写入失败')
        with open(self.__filepath, 'w', encoding='utf-8') as f:
            self.conf.write(f)

    def get_mode_path(self):
        return os.path.join(setting.FILE_PATH["MODEL"], self.get_section_for_data("MODEL", "name"))